﻿using Furion;
using Kevin.Code.Generator.DbAccessor.Models;
using SqlSugar;
using Sunny.UI;

namespace Kevin.Code.Generator.Forms
{
    public partial class AddDbLinkForm : UIEditForm
    {
        public AddDbLinkForm()
        {
            InitializeComponent();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(linkNameInput, "请输入链接的名称")
                   && CheckEmpty(dbTypeInput, "请输入选择数据库类型")
                   && CheckEmpty(connectionStringInput, "请输入链接地址");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnOK_Click(object sender, EventArgs e)
        {
            var linkName = this.linkNameInput.Text.Trim();
            var _repository = App.GetRequiredService<ISqlSugarRepository>().Context;
            var exists = await _repository.Queryable<DB_Connection>().AnyAsync(dc => dc.Name == linkName);
            if (exists)
            {
                ShowErrorTip("链接名称已存在,请修改后保存");
                return;
            }
            var dbType = this.dbTypeInput.Text.Trim();
            var connectionString = this.connectionStringInput.Text.Trim();
            await _repository.Insertable<DB_Connection>(new DB_Connection
            {
                Name = linkName,
                Type = (int)Enum.Parse(typeof(DbType), dbType),
                Connection_String = connectionString,
                Create_Time = DateTime.Now
            }).ExecuteCommandAsync();
        }

        private void AddDbLinkForm_Load(object sender, EventArgs e)
        {
            var allowDbTypes = new List<string> { "MySql", "SqlServer", "Sqlite", "Oracle", "PostgreSQL" };
            foreach (DbType type in Enum.GetValues(typeof(DbType)))
            {
                if (allowDbTypes.Any(c => string.Equals(type.ToString(), c, StringComparison.OrdinalIgnoreCase)))
                    this.dbTypeInput.Items.Add(type.ToString());
            }
        }

        private void testLinkButton_Click(object sender, EventArgs e)
        {
            var linkName = this.linkNameInput.Text.Trim();
            if (string.IsNullOrEmpty(linkName))
            {
                ShowWarningTip("请输入链接名称");
                return;
            }
            var dbType = this.dbTypeInput.Text.Trim();
            if (string.IsNullOrEmpty(dbType))
            {
                ShowWarningTip("请选择数据库类型");
                return;
            }
            var connectionString = this.connectionStringInput.Text.Trim();
            if (string.IsNullOrEmpty(connectionString))
            {
                ShowWarningTip("请输入数据库链接地址");
                return;
            }
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = (DbType)Enum.Parse(typeof(DbType),dbType),
                IsAutoCloseConnection = true
            });
            var linkSuccess = db.Ado.IsValidConnection();
            if (linkSuccess)
                ShowSuccessTip("数据库链接成功");
            else
                ShowErrorTip("数据库链接失败");
        }
    }
}
